﻿app.filter('startFrom', function () {
    return function (input, start) {
        if (input) {
            start = +start;
            return input.slice(start);
        }
        return [];
    }
});
app.controller('MagazineController', ['$scope', 'GlobalService', 'fileUpload', 'ApplicationService', 'alertboxservice', function ($scope, GlobalService, fileUpload, ApplicationService, alertboxservice) {

    $scope.Months = [{ monthId: "January", monthname: "January" }, { monthId: "February", monthname: "February" }
    , { monthId: "March", monthname: "March" }, { monthId: "Apirl", monthname: "Apirl" }
    , { monthId: "May", monthname: "May" }, { monthId: "June", monthname: "June" }
    , { monthId: "July", monthname: "July" }, { monthId: "Auguest", monthname: "Auguest" }
    , { monthId: "September", monthname: "September" }, { monthId: "October", monthname: "October" }
    , { monthId: "November", monthname: "November" }, { monthId: "December", monthname: "December" }]

    $scope.Years = [{ YearId: "2012", Yearname: "2012" }, { YearId: "2013", Yearname: "2013" },
    { YearId: "2014", Yearname: "2014" }, { YearId: "2015", Yearname: "2015" },
    { YearId: "2016", Yearname: "2016" }, { YearId: "2017", Yearname: "2017" }];

    BindGrid();

    $scope.toggle = function (modelstate, id) {
        $scope.Image = null;
        $scope.modelstate = modelstate;
        $scope.id = id;
        switch (modelstate) {
            case 'add':
                $scope.title = "Add Magazine";
                $scope.magazine = null;
                break;
            case 'edit':
                $scope.title = "Edit Magazine";
                url = "Magazine/GetMagazines";
                ApplicationService.Getdata(url, id).success(function (data) {
                    $scope.magazine = data;
                });
                break;
        }
        $("#Magazine").modal({ backdrop: 'static' });
    }

    function BindGrid() {
        url = "Magazine/GetMagazine";
        ApplicationService.Getdatas(url).success(function (data) {
            $scope.list = data;
            $scope.currentPage = 1;
            $scope.entryLimit = 10;
            $scope.filteredItems = $scope.list.length;
            $scope.totalItems = $scope.list.length;
        });
    }

    $scope.setPage = function (pageNo) {
        $scope.currentPage = pageNo;
    };

    $scope.sort_by = function (predicate) {
        $scope.predicate = predicate;
        $scope.reverse = !$scope.reverse;
    };

    $scope.delete = function (data, dataid) {
        swal({
            title: "Are you sure?",
            text: "Your will not be able to recover " + data + " Details!",
            type: "warning",
            showCancelButton: true,
            cancelButtonClass: 'btn-outline btn-black mr-10',
            cancelButtonText: 'Cancel',
            confirmButtonClass: "btn-danger",
            confirmButtonText: "Delete",
            closeOnConfirm: false
        }, function (status) {
            if (status) {
                url = "Magazine/DeleteMagazine";
                ApplicationService.DeleteData(url, dataid, data).success(function (data) {
                    if (data == "DSUS") {
                        swal("Deleted!", "Details deleted successfully", "success");
                    }
                    else {
                        swal("failed", "Unable to delete this details", "error");
                    }
                    BindGrid();
                });
            }
        });
    }

    $scope.save = function (modelstate, id) {
        debugger;
        $scope.buttonstate = true;
        var url;
        switch (modelstate) {
            case 'add':
                url = "Magazine/AddMagazine";
                break;
            case 'edit':
                url = "Magazine/UpdateMagazine";
                break;
        }
        savedetails(url, $scope.magazine);
        
    }

    $scope.FileMagazine = function (evt) {
        var file = evt.currentTarget.files[0];
        var reader = new FileReader();
        reader.onload = function (evt) {
            $scope.$apply(function ($scope) {
                alert();
                $scope.magazine.MagazineStartImage = evt.target.result;
            });
        };
        reader.readAsDataURL(file);
    }

    function savedetails(url, data) {
        ApplicationService.Save($scope.magazine, url).success(function (result) {
            alertboxservice.ShowMsg(result, "Magazine ");
            $scope.buttonstate = false;
            BindGrid();
        });
    }
}]);

